package com.aj.service;

import com.aj.Interface.OrderService;
import com.aj.common.MessageConstant;
import com.aj.dao.MemberDao;
import com.aj.dao.OrderDao;
import com.aj.dao.OrderSettingDao;
import com.aj.pojo.Member;
import com.aj.pojo.Order;
import com.aj.pojo.OrderSetting;
import com.aj.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;


    /**
     * 预约
     * @param map
     * @return
     */
    @Override
    public Result submit(Map map) throws Exception {
        //判断当前时间是否可以预约
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingDao.getOrderSettingByDate(date);
        if (orderSetting==null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //判断预约时间人数是否已满
        if (orderSetting.getNumber()==orderSetting.getReservations()){
            return new Result(false, MessageConstant.ORDER_FULL);

        }
        //判断用户是不是会员
        String telephone = (String) map.get("telephone");
        Member member;
       member = memberDao.findMemberByTelephone(telephone);
      //如果是会员,查询是否预约过
      if (member!=null){
          Map orderMap = new HashMap();
          Integer memberid = member.getId();
          int setmealId = Integer.parseInt((String) map.get("setmealId"));
          orderMap.put("memberid",memberid );
          orderMap.put("date",date );
          orderMap.put("setmealId",setmealId );
       List<Order> order = orderDao.getOrderByMemberidDateSetmealId(orderMap);
          //如果预约过,不能重复预约
        if (order!=null&& order.size()>0){
            return new Result(false,MessageConstant.HAS_ORDERED);
        }
        //如果没预约,就预约(下面统一预约)

      }else {
          //如果不是会员,自动注册成会员,
//          memberMap.put(, )
//
          member = new Member();
          member.setName((String) map.get("name"));
          member.setSex((String) map.get("sex"));
          member.setIdCard((String) map.get("idCard"));
          member.setPhoneNumber((String) map.get("telephone"));
          member.setRegTime(new Date());
           memberDao.addMember(member);
        //注册成会员再添加预约信息(下面统一预约)

      }
        //添加预约信息
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(date);
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        int setmealId = Integer.parseInt((String) map.get("setmealId"));
        order.setSetmealId(setmealId);
        orderDao.addorder(order);

        //预约成功,更新当天预约人数
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingDao.updateReservations(orderSetting);

        return new Result(true,MessageConstant.ORDER_SUCCESS,order);
    }



    /**
     * 根据id查询预约信息
     * @param id
     * @return
     */
    @Override
    public Map findById(Integer id) {
     Map map =  orderDao.findById(id);

        return map;
    }
}
